package com.billjc.activity.config.db;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = DateSourceFor400Config.PACKAGE, sqlSessionFactoryRef = "call400SqlSessionFactory")
public class DateSourceFor400Config {
    static final String PACKAGE = "com.billjc.activity.dao.master";
    /**
     * 400数据源
     * @return
     */
	@ConfigurationProperties(prefix = "spring.call400-datasource")
	@Bean(name="call400DataSource")
	public DataSource call400DataSource() {
		return new DruidDataSource();
	}
	//数据源事务管理器
    @Bean(name="call400DataSourceTransactionManager")
    public DataSourceTransactionManager call400DataSourceTransactionManager(){
        return new DataSourceTransactionManager(call400DataSource());
    }

    //创建Session
    @Bean(name="call400SqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("call400DataSource") DataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        return sessionFactory.getObject();
    }
}
